using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Rocket.Core.Common
{
    public class Encryption 
    {
        private Byte[] KEY_64 = { 1, 2, 3, 4, 5, 6, 7, 8 };
        private Byte[] IV_64 = { 8, 7, 6, 5, 4, 3, 2, 1 };

        /// <summary>
        /// This constructor will create a new instance 
        /// of the Encryption class
        /// </summary>
        public Encryption()
        {
        }

        /// <summary>
        /// This method will encrypt a string value
        /// </summary>
        /// <param name="value">The string value to encrypt</param>
        /// <returns>Returns DES encrypted string or string.empty upon error</returns>
        public string DoEncrypt(string sValue)
        {
            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);

                sw.Write(sValue);
                sw.Flush();
                cs.FlushFinalBlock();
                ms.Flush();

                // convert back to a string
                return Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
            }
            catch { }

            return string.Empty;
        }

        /// <summary>
        /// This method will decrypt a DES encrypted string value
        /// </summary>
        /// <param name="strValue">The encrypted string value to decrypt</param>
        /// <returns>Returns DES decrypted string or string.empty upon error </returns>
        public string DoDecrypt(string sValue)
        {
            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                Byte[] buffer = Convert.FromBase64String(sValue);
                MemoryStream ms = new MemoryStream(buffer);
                CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);

                return sr.ReadToEnd();
            }
            catch { }

            return string.Empty;

        }

        /// <summary>
        /// One way Encrypyion
        /// </summary>
        /// <param name="password"></param>
        /// <returns>encripted password or string.empty upon error</returns>
        public  string GetEncryptedPassword(string password)
        {
            string encryptedPass = string.Empty;

            try
            {
                //Convert the password string into an Array of bytes.
                UTF8Encoding textConverter = new UTF8Encoding();
                byte[] passBytes = textConverter.GetBytes(password);
                byte[] result;

                SHA384Managed t = new SHA384Managed();
                result = t.ComputeHash(passBytes);
                encryptedPass = Convert.ToBase64String(result);
            }
            catch { }

            return encryptedPass;

        }
    }
}
